package com.cjm.one.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjm.one.common.annotation.IgnoreTenantForAdmin;
import com.cjm.one.common.result.Result;
import com.cjm.one.model.base.PasswordDto;
import com.cjm.one.model.system.dto.SysUserInfoDto;
import com.cjm.one.model.system.dto.SysUserInfoFromDto;
import com.cjm.one.model.system.entity.SysUserInfo;
import com.cjm.one.model.system.entity.SysUserRole;
import com.cjm.one.model.system.vo.SysUserInfoVo;
//import com.cjm.one.security.annotation.Within;
import com.cjm.one.security.annotation.Within;
import com.cjm.one.system.service.SysUserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.BeanUtils;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "系统用户管理", description = "系统用户管理")
@Slf4j
@RequestMapping("/admin/system")
public class SysUserInfoController {

    @Resource
    private SysUserInfoService sysUserInfoService;


    @Operation(summary = "用户分页")
    @PostMapping("/sysUser/page")
    @IgnoreTenantForAdmin
    public Result<Page<SysUserInfoVo>> getSysUserPage( @RequestBody SysUserInfoFromDto queryVo) {
        log.info("获取用户列表参数{}", queryVo);
        Page<SysUserInfoVo> page = new Page<>(queryVo.getCurrent(), queryVo.getSize());
        Page<SysUserInfoVo> result = sysUserInfoService.getSysUserPage(page, queryVo);
        return Result.ok(result);
    }


    @GetMapping("/sysUser/{id}")
    @Operation(summary = "根据id获取系统用户信息")
    @IgnoreTenantForAdmin
    public Result<SysUserInfoVo> getSysUserById(@PathVariable("id") Long id) {
        log.info("根据id获取系统用户信息{}", id);
        SysUserInfo sysUserInfo = sysUserInfoService.getSysUserById(id);
        SysUserInfoVo sysUserInfoVo = new SysUserInfoVo();
        if (sysUserInfo != null) {
            BeanUtils.copyProperties(sysUserInfo, sysUserInfoVo);
        }
        log.info("获取用户信息{}", sysUserInfoVo);
        return Result.ok(sysUserInfoVo);
    }
    @Within
    @GetMapping("/sysUser/name/{username}")
    @Operation(summary = "根据用户名获取系统用户信息")
    @IgnoreTenantForAdmin
    public Result<SysUserInfo> getSysUserByName(@PathVariable("username") String username) {
        log.info("根据用户名获取系统用户信息{}", username);
        SysUserInfo sysUserInfo = sysUserInfoService.getByUserName(username);
        log.info("获取用户信息{}", sysUserInfo);
        return Result.ok(sysUserInfo);
    }

    @PostMapping("/sysUser")
    @Operation(summary = "新增系统用户")
    @IgnoreTenantForAdmin
    public Result<Boolean> addSysUser(@RequestBody SysUserInfo sysUserInfo) {
        log.info("新增系统用户sysUserInfo:{}", sysUserInfo);

        String password = sysUserInfo.getPassword();
        if (password == null || password.isEmpty()) {
            //初始密码
            password = "Cjm123456";
        }
        PasswordEncoder customPasswordEncoder = new BCryptPasswordEncoder();
        String encodePassword = customPasswordEncoder.encode(password);
        sysUserInfo.setPassword(encodePassword);
        boolean flag = sysUserInfoService.saveUser(sysUserInfo);
        return Result.ok(flag);
    }
    @DeleteMapping("/sysUser/{id}")
    @Operation(summary = "根据id删除系统用户")
    @IgnoreTenantForAdmin
    public Result<Boolean> deleteSysUser(@PathVariable("id") Long id) {
        log.info("根据id删除系统用户{}", id);
        boolean flag = sysUserInfoService.deleteSysUser(id);
        return Result.ok(flag);
    }
    @PutMapping("/sysUser")
    @Operation(summary = "根据id修改系统用户")
    @IgnoreTenantForAdmin
    public Result<Boolean> updateSysUser(@RequestBody SysUserInfoDto sysUserInfoDto) {
        log.info("根据id修改系统用户sysUserInfoDto:{}", sysUserInfoDto);
        boolean flag = sysUserInfoService.updatSysUsereById(sysUserInfoDto);
        return Result.ok(flag);
    }
    @PutMapping("/sysUser/password")
    @Operation(summary = "根据id修改系统用户密码")
    @IgnoreTenantForAdmin
    public Result<Boolean> updateSysUserPassword(@RequestBody PasswordDto passwordDto) {
        log.info("根据id修改系统用户密码passwordDto:{}", passwordDto);
        boolean flag = sysUserInfoService.updateSysUserePasswordById(passwordDto);
        return Result.ok(flag);
    }
    @Operation(summary = "批量删除用户")
    @DeleteMapping("/sysUser/deleteMore")
    @IgnoreTenantForAdmin
    public Result<Integer> deleteSysUserMore( @RequestBody List<Long> ids) {
        log.info("批量删除用户ids:{}", ids);
        Integer flag = sysUserInfoService.deleteSysUsereMore(ids);
        return Result.ok(flag);
    }

    @PutMapping("/sysUser/status")
    @Operation(summary = "根据id修改系统用户状态")
    @IgnoreTenantForAdmin
    public Result<Boolean> updateSysUserStatus(@RequestBody SysUserInfoDto sysUserInfoDto) {
        log.info("根据id修改系统用户状态sysUserInfoDto:{}", sysUserInfoDto);
        boolean flag = sysUserInfoService.updateSysUsereStatusById(sysUserInfoDto);
        return Result.ok(flag);
    }
    @GetMapping("/sysUser/roleByUserId/{userId}")
    @Operation(summary = "根据用户id获取角色list")
    @IgnoreTenantForAdmin
    public Result<List<SysUserRole>> getSysUserRole(@PathVariable("userId") Long userId) {
        log.info("根据用户id获取角色{}", userId);
        List<SysUserRole> role = sysUserInfoService.getSysUserRoleList(userId);
        return Result.ok(role);
    }
    @PutMapping("/sysUser/resetPassword")
    @Operation(summary = "重置密码")
    @IgnoreTenantForAdmin
    public Result<Boolean> resetPassword(@RequestBody PasswordDto passwordDto) {
        return Result.ok(sysUserInfoService.resetPassword(passwordDto));
    }
}
